<template>
  <div class="dialog">
    <el-dialog
      :title="info.isadd ? '添加菜单' : '编辑菜单'"
      :before-close="cancel"
      :visible.sync="info.isshow"
    >
      <el-form :model="userForm" ref="user"  :rules="rules">
        <el-form-item  prop="roleid" label="所属角色" :label-width="formLabelWidth">
          <el-select v-model="userForm.roleid" >
            <el-option label="请选择" :value="0" disabled>==请选择==</el-option>
            <el-option
              v-for="item in rolelist"
              :key="item.id"
              :label="item.rolename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="username" label="管理员名称" :label-width="formLabelWidth">
       <el-input v-model="userForm.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth">
          <el-input v-model="userForm.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="userForm.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button @click="addInfo" v-if="info.isadd" type="primary"
          >添 加</el-button
        >
        <el-button @click="editInfo" v-else type="primary" ref="user">编 辑</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {mapActions,mapGetters} from 'vuex'
import { userAdd, userInfo, userEdit } from "../../util/request/api";
export default {
  props: ["info"],
  data() {
    return {
      
       rules: {
        title: [
          { required: true, message: "请输入菜单名称", trigger: "blur" },
          { min: 2, max: 6, message: "长度在 2 到 6 个字符", trigger: "blur" },
        ],
         roleid: [
          { required: true, message: "请选择所属角色", trigger: "change" },
        ],
      },
      userForm: {
        roleid: 0,
        username: "",
        password: "",
        status: 1,
      },
      formLabelWidth: "120px",
    };
  },
 
  computed: {
    ...mapGetters({
      userlist: "user/getUserList",
       rolelist: "role/getRoleList",
    }),
  },
   mounted() {
    this.getUserList()
    this.getRolelist();
  },
  methods: {
    ...mapActions({
      getRolelist: "role/getRoleAction",
      getUserList: "user/getUserAction",
      getCount: "user/getCountAction",
    }),
    cancel() {
      this.$emit("cancel", false);
      this.empty();
    },
    //清空输入框
    empty() {
      this.userForm={
        roleid: 0,
        username: "",
        password: "",
        status: 1,
      }
    },
    addInfo() {
      this.$refs.user.validate((valid) => {
        if (valid) {
          userAdd(this.userForm).then((res) => {
            console.log(this.userForm);
            if (res.code == 200) {
              
              this.$message.success(res.msg);
              this.cancel();
              this.getUserList();
              this.getCount();
            } else {
              this.$message.error(res.msg);
               }  
          });
        }
      });
    },
    lookup(uid) {
      userInfo({ uid }).then((res) => {
        if (res.code == 200) {
          console.log(res, "一条数据");
          this.userForm = res.list;
          this.userForm.uid = uid;
        }
      });
    },
   editInfo() {
      this.$refs.user.validate((valid) => {
        if (valid) {
          //调取接口
          userEdit(this.userForm).then((res) => {
            if (res.code == 200) {
              this.$message.success(res.msg);
              //成功之后 关闭弹框
              this.cancel();
              //添加成功之后，要重新调用一下列表接口，重新触发行动
              this.getUserList();
              this.getCount();
            } else {
              this.$message.error(res.msg);
            }
          });
        } else {
        }
      });
    },
  },
};
</script>

<style scoped>
.dialog {
  position: absolute;
  top: 50%;
  left: 50%;
}
